/****************************************************************************
 * resize.c
 * 
 * Arcadie Condrat
 * Computer Science 50
 * Problem Set 4
 *
 * Resizes a BMP image by the factor on n
 ***************************************************************************/
       
#include <stdio.h>
#include <stdlib.h>
#include "bmp.h"

int main(int argc, char* argv[])
{
    // ensure proper usage
    if (argc != 4)
    {
        printf("Usage: resize n infile outfile\n");
        return 1;
    }
    
    int n = atoi(argv[1]);
	if (n < 1 || n > 100)
	{
        printf("Invalid resize factor!\nn must be a positive integer less than or equal to 100\n");
        return 1;
    }
    
    // remember filenames
    char* infile = argv[2];
    char* outfile = argv[3];

    // open input file 
    FILE* inptr = fopen(infile, "r");
    if (inptr == NULL)
    {
        printf("Could not open %s.\n", infile);
        return 2;
    }

    // open output file
    FILE* outptr = fopen(outfile, "w");
    if (outptr == NULL)
    {
        fclose(inptr);
        fprintf(stderr, "Could not create %s.\n", outfile);
        return 3;
    }

    // read infile's BITMAPFILEHEADER
    BITMAPFILEHEADER bf;
    fread(&bf, sizeof(BITMAPFILEHEADER), 1, inptr);

    // read infile's BITMAPINFOHEADER
    BITMAPINFOHEADER bi;
    fread(&bi, sizeof(BITMAPINFOHEADER), 1, inptr);

    // ensure infile is a 24-bit uncompressed BMP 4.0
    if (bf.bfType != 0x4d42 || bf.bfOffBits != 54 || bi.biSize != 40 || 
        bi.biBitCount != 24 || bi.biCompression != 0)
    {
        fclose(outptr);
        fclose(inptr);
        fprintf(stderr, "Unsupported file format.\n");
        return 4;
    }
	  
	// determine padding for scanlines (original file)
    int padding =  (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;    
    
    
    // determine new image width and height
    bi.biWidth = bi.biWidth * n;
    bi.biHeight = bi.biHeight * n;
  
    // determine padding for scanlines (new file)
    int new_padding = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
	
	// update biSizeImage and bfSize
    bi.biSizeImage = (bi.biWidth * sizeof(RGBTRIPLE) + new_padding) * abs(bi.biHeight);
    bf.bfSize = bf.bfOffBits + bi.biSizeImage;
   
    // write outfile's BITMAPFILEHEADER
    fwrite(&bf, sizeof(BITMAPFILEHEADER), 1, outptr);

    // write outfile's BITMAPINFOHEADER
    fwrite(&bi, sizeof(BITMAPINFOHEADER), 1, outptr);

	
    // iterate over infile's scanlines
    int biHeight = abs(bi.biHeight) / n;
    int biWidth = bi.biWidth / n;  
    for (int i = 0; i < biHeight; i++)
    {
		// copy the same scanline n times
		for (int k = 0; k < n; k++)
        {
			// iterate over pixels in scanline
			for (int j = 0; j < biWidth; j++)
			{
				// temporary storage
				RGBTRIPLE triple;	

				// read RGB triple from infile
				fread(&triple, sizeof(RGBTRIPLE), 1, inptr);

				// write RGB triple to outfile
				// copy the same pixel n times
				for (int m  = 0; m < n; m++)
				{
					fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
				}
			}
			
			// add padding to the new file
			for (int k = 0; k < new_padding; k++)
			{
				fputc(0x00, outptr);
			}
			
			// return cursor to the beggining of the scanline
			fseek(inptr, -(biWidth * sizeof(RGBTRIPLE)), SEEK_CUR);
		}
		
		// move cursor to the next scanline
		fseek(inptr, (biWidth * sizeof(RGBTRIPLE)), SEEK_CUR);
		
		// skip over padding, if any
		fseek(inptr, padding, SEEK_CUR);
    }

    // close infile
    fclose(inptr);

    // close outfile
    fclose(outptr);

    return 0;
}
